package MaintMenu.VenPricingUpdMasterFiles;

import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class TYGtw {

    public TYGtw() {

        int cc=0;
        SQLUpdStm stm = new SQLUpdStm();

        String tableA, tableB,onA,onB, whereA,whereB,whereoperator,
                fieldA1,fieldB1,
                fieldA2,fieldB2,
                fieldA3,fieldB3,
                fieldA4,fieldB4,
                fieldA5,fieldB5,
                fieldA6,fieldB6,
                fieldA7,fieldB7,
                fieldA8,fieldB8,
                fieldA9,fieldB9,
                fieldA10,field10;

        int filesToUpdate=1;
        while (filesToUpdate<=5){


            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String dataSourceName = "dbLI";
                String dbURL = (new StringBuilder()).append("jdbc:odbc:").append(dataSourceName).toString();
                Connection conli = DriverManager.getConnection(dbURL, "", "");


                String st[] = new String[6];


                whereA="";
                whereB="";
                whereoperator="";
                tableA =    "mirrorm1";
                tableB =    "_COSTTYGTW"+filesToUpdate;
                onA=        "partlink";
                onB=        "PARTSLINK";

                fieldA1=    "part_no";       //part_no  (oem#)
                fieldB1=    "[OE#]";

                fieldA2=    "TYG_TW_NO";       //DEPOT #
                fieldB2=    "[TY#]";

                fieldA3=    "TYG_TW_PRC";      //PRICE
                fieldB3=    "PRICE";

                fieldA4=    "TYG_TW_PKG";       //Package
                fieldB4=    "[STD QTY]";

                fieldA5=    "TYG_CUFT";         //Cu/FT
                fieldB5=    "[CU/FT]";
                st[0] = stm.StmExecute(tableA,tableB,whereA,whereB,whereoperator,onA,onB,fieldA1,fieldB1,fieldA2,fieldB2,fieldA3,fieldB3,fieldA4,fieldB4,fieldA5,fieldB5);
    //            System.out.println(st[0]);

                             /* update Cooling master file */
                whereA="";
                whereB="";
                whereoperator="";
                tableA =    "coolngm";
                tableB =    "_COSTTYGTW"+filesToUpdate;
                onA=        "partlink";
                onB=        "PARTSLINK";

                fieldA1=    "part_no";       //part_no  (oem#)
                fieldB1=    "[OE#]";

                fieldA2=    "TW_TYGNO";       //DEPOT #
                fieldB2=    "[TY#]";

                fieldA3=    "TW_TYGPRC";      //PRICE
                fieldB3=    "PRICE";

                fieldA4=    "TW_TYGPKG";      //Package
                fieldB4=    "[STD QTY]";

                fieldA5=    "TYG_CUFT";       //Cu/FT
                fieldB5=    "[CU/FT]";
                st[1] = stm.StmExecute(tableA,tableB,whereA,whereB,whereoperator,onA,onB,fieldA1,fieldB1,fieldA2,fieldB2,fieldA3,fieldB3,fieldA4,fieldB4,fieldA5,fieldB5);
    //            System.out.println(st[1]);

                 /* update HFMAST1 master file */
                whereA="";
                whereB="";
                whereoperator="";
                tableA =    "HFMAST";
                tableB =    "_COSTTYGTW"+filesToUpdate;
                onA=        "partlink";
                onB=        "PARTSLINK";

                fieldA1=    "part_no";       //part_no  (oem#)
                fieldB1=    "[OE#]";

                fieldA2=    "TYG_NO";       //DEPOT #
                fieldB2=    "[TY#]";

                fieldA3=    "TYG_PRC";      //PRICE
                fieldB3=    "PRICE";

                fieldA4=    "TYG_PKG";      //Package
                fieldB4=    "[STD QTY]";

                fieldA5=    "TYG_CUFT";      //Cu/FT
                fieldB5=    "[CU/FT]";
                st[2] = stm.StmExecute(tableA,tableB,whereA,whereB,whereoperator,onA,onB,fieldA1,fieldB1,fieldA2,fieldB2,fieldA3,fieldB3,fieldA4,fieldB4,fieldA5,fieldB5);
    //            System.out.println(st[2]);

                /* update OTMAST1 master file */
                whereA="";
                whereB="";
                whereoperator="";
                tableA =    "OTMAST";
                tableB =    "_COSTTYGTW"+filesToUpdate;
                onA=        "partlink";
                onB=        "PARTSLINK";

                fieldA1=    "part_no";       //part_no  (oem#)
                fieldB1=    "[OE#]";

                fieldA2=    "TYG_NO";       //DEPOT #
                fieldB2=    "[TY#]";

                fieldA3=    "TYG_PRC";      //PRICE
                fieldB3=    "PRICE";

                fieldA4=    "TYG_PKG";       //Package
                fieldB4=    "[STD QTY]";

                fieldA5=    "TYG_CUFT";     //Cu/FT
                fieldB5=    "[CU/FT]";
                st[3] = stm.StmExecute(tableA,tableB,whereA,whereB,whereoperator,onA,onB,fieldA1,fieldB1,fieldA2,fieldB2,fieldA3,fieldB3,fieldA4,fieldB4,fieldA5,fieldB5);
    //            System.out.println(st[3]);

                /* update BCMAST1 master file with CAPA */
    //             WHERE a.capa=true AND b.CAPA=TRUE;");
    //
    //             WHERE (a.capa is null or 'n') AND b.CAPA=FALSE;");
                whereA="WHERE A.CAPA=YES";
                whereB="B.CAPA=TRUE";
                whereoperator="AND";
                tableA =    "BCMAST";
                tableB =    "_COSTTYGTW"+filesToUpdate;
                onA=        "partlink";
                onB=        "PARTSLINK";

                fieldA1=    "part_no";       //part_no  (oem#)
                fieldB1=    "[OE#]";

                fieldA2=    "TYG_NO";       //DEPOT #
                fieldB2=    "[TY#]";

                fieldA3=    "TYG_PRC";      //PRICE
                fieldB3=    "PRICE";

                fieldA4=    "TYG_PKG";       //Package
                fieldB4=    "[STD QTY]";

                fieldA5=    "TYG_CUFT";    //Cu/FT
                fieldB5=    "[CU/FT]";
                st[4] = stm.StmExecute(tableA,tableB,whereA,whereB,whereoperator,onA,onB,fieldA1,fieldB1,fieldA2,fieldB2,fieldA3,fieldB3,fieldA4,fieldB4,fieldA5,fieldB5);
    //            System.out.println(st[4]);

                /* update BCMAST1 master file NOT CAPA */
    //             WHERE a.capa=true AND b.CAPA=TRUE;");
    //
    //             WHERE (a.capa is null or 'n') AND b.CAPA=FALSE;");
                whereA="";
                whereB="";
                whereoperator="";
    //            whereA="WHERE A.CAPA IS NULL";
    //            whereB="B.CAPA=FALSE";
    //            whereoperator="AND";
                tableA =    "BCMAST";
                tableB =    "_COSTTYGTW"+filesToUpdate;
                onA=        "partlink";
                onB=        "PARTSLINK";

                fieldA1=    "part_no";       //part_no  (oem#)
                fieldB1=    "[OE#]";

                fieldA2=    "TYG_NO";       //DEPOT #
                fieldB2=    "[TY#]";

                fieldA3=    "TYG_PRC";      //PRICE
                fieldB3=    "PRICE";

                fieldA4=    "TYG_PKG";       //Package
                fieldB4=    "[STD QTY]";

                fieldA5=    "TYG_CUFT";      //Cu/FT
                fieldB5=    "[CU/FT]";
                st[5] = stm.StmExecute(tableA,tableB,whereA,whereB,whereoperator,onA,onB,fieldA1,fieldB1,fieldA2,fieldB2,fieldA3,fieldB3,fieldA4,fieldB4,fieldA5,fieldB5);
    //            System.out.println(st[5]);

                for (int x=0; x<st.length;x++){
                    Statement stli = conli.createStatement();

                    try {

//                        System.out.println("Script :"+x);
//                        System.out.println(st[x]);
                        int rowsupdated = stli.executeUpdate(st[x]);
                        System.out.print("TRU : "+rowsupdated+"\t");
                        //JOptionPane.showOptionDialog(null, "Statement : " + st[x] + "\n\nTotal record Updated : " + rowsupdated, "Update Complete", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);

                        cc++;
                        if (cc==4){
                            System.out.println("");
                            cc=0;
                        }

                    }catch(SQLException er) {
                        er.printStackTrace();
                    }stli.close();
                }



            }
            catch(Exception e)
            {
                e.printStackTrace();
            }filesToUpdate++;
        }
    }
}
